<?php

/**
 * uniBoard API Imageset
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_imageset extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore			= NULL;
	
	/**
	 * Imageset ID
	 *
	 * @var integer
	 */
	
	public $imageset_id 		= 0;
	
	/**
	 * Imageset Name
	 *
	 * @var string
	 */
	
	public $imageset_name		= '';
	
	/**
	 * Imageset Author Name
	 *
	 * @var string
	 */
	
	public $imageset_author	= '';
		
	/**
	 * Imageset Author E-Mail
	 *
	 * @var string
	 */
	
	public $imageset_email		= '';
	
	/**
	 * Imageset Author WWW
	 *
	 * @var string
	 */
	
	public $imageset_www		= '';
			
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store imageset id
		$this -> imageset_id = (integer) $target_id;
		
		// Autoload If Imageset ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'imagesets
			WHERE imageset_id = \'' . $this -> imageset_id . '\'
			LIMIT 1');
			
			// Style found
			if ( $style_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded			= true;
				
				$this -> imageset_name 			= $style_result['imageset_name'];
				$this -> imageset_author 		= $style_result['imageset_author'];
				$this -> imageset_email			= $style_result['imageset_email'];
				$this -> imageset_www 			= $style_result['imageset_www'];
			}
			else
			{
				//give action an error
				$this -> setError(  $appCore -> lang['acp_imageset_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> imageset_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'imagesets');
			
				// Store new imageset id
				$this -> imageset_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'imagesets', 'imageset_id = \'' . $this -> imageset_id . '\'');
			
				// Flush cache
				class_cache::flushCache( 'imageset_' . $this -> imageset_id);
			}
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data from DB and filesystem
	 *
	 */
	
	function deleteData(){
		
		// Delete set's images directory
		if ( is_dir( STYLES_DATA_PATH . 'imageset_' . $this -> imageset_id . '/'))
		{
			// Use core function?
			if ( USE_GLOB)
			{
				class_files::deleteDir( STYLES_DATA_PATH . 'imageset_' . $this -> imageset_id . '/');
			}
			else
			{
				// Delete Index File
				if ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> imageset_id . '/index.html'))
				{
					unlink( STYLES_DATA_PATH . 'imageset_' . $this -> imageset_id . '/index.html');
				}
				
				// Select Imageset Files
				$this -> appCore -> db -> query( 'SELECT image_file FROM ' . DB_PREFIX . 'imagesets_images WHERE imageset_id = \'' . $this -> imageset_id . '\'');
				
				// Reiterate files
				while ( $file_result = $appCore -> db -> fetch_array())
				{
					if ( file_exists( STYLES_DATA_PATH . 'imageset_' . $this -> imageset_id . '/' . $file_result[0])){
						// Unlink file
						unlink( STYLES_DATA_PATH . 'imageset_' . $this -> imageset_id . '/' . $file_result[0]);
					}
				}
				
				// Free result
				$appCore -> db -> freeResult();
			}		
		}
		
		// Delete set's cache
		class_cache::flushCache( 'imageset_' . $this -> imageset_id);
		
		// Delete records in database
		$this -> appCore -> db -> delete( 'imagesets', 'imageset_id = \'' . $this -> imageset_id . '\'');
		$this -> appCore -> db -> delete( 'imagesets_images', 'imageset_id = \'' . $this -> imageset_id . '\'');
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set imageset_name
	 *
	 * @param string $new_value
	 */
	
	function setImagesetName( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> imageset_name = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_imageset_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['imageset_name'] = $this -> appCore -> db -> strEscape( $this -> imageset_name);
			}
		}
		else
		{
			// Set error
			$this -> setError(  $this -> appCore -> lang['acp_imageset_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set imageset_author
	 *
	 * @param string $new_value
	 */
	
	function setImagesetAuthor( $new_value){
		
		// Store in object
		$this -> imageset_author = $new_value;
		
		// Check max lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_imageset_author_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['imageset_author'] = $this -> appCore -> db -> strEscape( $this -> imageset_author);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set imageset_email
	 *
	 * @param string $new_value
	 */
	
	function setImagesetEmail( $new_value){
		
		// Store in object
		$this -> imageset_email = $new_value;
		
		// Check max lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_imageset_email_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['imageset_email'] = $this -> appCore -> db -> strEscape( $this -> imageset_email);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set imageset_www
	 *
	 * @param string $new_value
	 */
	
	function setImagesetWww( $new_value){
		
		// Store in object
		$this -> imageset_www = $new_value;
		
		// Check max lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_imageset_www_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['imageset_www'] = $this -> appCore -> db -> strEscape( $this -> imageset_www);
		}
		
	}
	
}